package com.srz.dpm.core.controller.api;

import com.srz.common.exception.Assert;
import com.srz.common.exception.BusinessException;
import com.srz.common.result.R;
import com.srz.common.result.ResponseEnum;
import com.srz.dpm.core.pojo.entity.ProductParameters;
import com.srz.dpm.core.service.ProductParametersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 产品基本信息表 前端控制器
 * </p>
 *
 * @author srz
 * @since 2021-09-09
 */
//跨域
//@CrossOrigin
@RestController
@Slf4j
@Api(tags = "参数列表的CRUD")
@RequestMapping("/api/core/productParameters")
@SuppressWarnings("all")
public class ProductParametersController {


    /**
     * Service 自动注入
     */
    @Resource
    private ProductParametersService productParametersService;
    @Autowired
    private ProductParametersService productParametersService_auto;


    /**
     * 产品信息列表
     * @return
     */
/*    @ApiOperation(value = "产品信息列表，传统返回值方式")
    @GetMapping("listAll")
    public List<ProductParameters> listAll(){
        log.info("数据展示成功");
        return productParametersService.list();
    }*/
    @ApiOperation(value = "产品信息列表，状态码R类型")
    @GetMapping("listAll_R")
    public R listAll_R(){
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userIp = req.getRemoteAddr();
        //String ipAddr = IpUtils.getIpAddr(request);
        log.info("ip为："+userIp+" 用户访问了");
        log.info("数据展示成功");
        log.warn("warning!!!");
        log.error("it's a error");


        List<ProductParameters> list = productParametersService.list();
        return R.ok().data("list",list).message("数据展示成功");
        //return R.setResult(ResponseEnum.SUCCESS).data("list",list).message("数据展示成功");
    }

    /**
     * 根据id查询产品信息列表
      * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询产品信息列表")
    @GetMapping("/get/{id}")
    public R getById(
            @ApiParam(value = "数据id", required = true, example = "7")
            @PathVariable Long id
    ) {
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userIp = req.getRemoteAddr();
        log.info("ip为："+userIp+" 用户访问了");


        ProductParameters productParameters = productParametersService.getById(id);

        if (productParameters != null){

            log.info("数据展示成功");
            return R.ok().message("数据展示成功").data("list",productParameters);
        } else {
            log.info("数据展示成功");
            return R.error().message("数据获取失败");
        }
    }


    /**
     * 产品信息列表的删除
     * @param id
     * @return
     */
    @ApiOperation(value = "产品信息列表的删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(@PathVariable Long id){

        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userIp = req.getRemoteAddr();
        log.info("ip为："+userIp+" 用户访问了");

        boolean b = productParametersService.removeById(id);
        if (b){
            log.info("数据删除成功");

           return R.ok().message("数据删除成功");
        }
        else {
            log.info("数据删除失败");

            return R.ok().message("数据删除失败");
        }
     }

    /**
     * 产品信息列表添加
     * @param productParameters
     * @return
     */
    @SneakyThrows
    @ApiOperation(value = "产品信息列表添加")
    @PostMapping("/save")
    public R save(@RequestBody ProductParameters productParameters){
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userIp = req.getRemoteAddr();
        log.info("ip为："+userIp+" 用户访问了");

        Assert.Null(productParameters.getName(),ResponseEnum.PRODECT_NAME_NULL_ERROR);
        System.out.println(productParameters.getName());

        boolean save = productParametersService.save(productParameters);
        if (save){
            log.info("数据添加成功");
            return R.ok().message("数据添加成功");
        }else {
            return R.ok().message("数据添加失败");
        }
    }

    /**
     * 产品信息列表的修改
     * @param productParameters
     * @return
     */
    @ApiOperation(value = "产品信息列表的修改")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(value = "产品参数信息", required = true, example = "7")
            @RequestBody ProductParameters productParameters){
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userIp = req.getRemoteAddr();
        log.info("ip为："+userIp+" 用户访问了");


        boolean b = productParametersService.updateById(productParameters);
        if (b){
            log.info("数据更新成功");
            return R.ok().message("数据更新成功");
        }else {
            log.info("数据更新失败");
            return R.ok().message("数据更新失败");
        }
    }



}